﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DLULearningNetworkModel
{
    public enum EnumFriendNotificationType
    {
        Invite=1,
        Accept
    }
    public partial class FriendNotification
    {

        public static int Delete(Guid friendNotificationID, Guid accountID)
        {
            try
            {
                using (DLULNEntities db = new DLULNEntities())
                {
                    FriendNotification friendNotification = db.FriendNotifications.SingleOrDefault(fn => fn.FriendNotificationID_u == friendNotificationID);
                    if (friendNotification != null && friendNotification.AccountID_u == accountID)
                    {
                        db.FriendNotifications.DeleteObject(friendNotification);
                        if (db.SaveChanges() > 0)
                        {
                            return 1;
                        }
                    }
                    return 0;
                }
            }
            catch
            {
                return 101;
            }
        }

        public static int UpdateReadState(Guid friendNotificationID, Guid accountID)
        {
            try
            {
                using (DLULNEntities db = new DLULNEntities())
                {
                    FriendNotification friendNotification = db.FriendNotifications.SingleOrDefault(fn => fn.FriendNotificationID_u == friendNotificationID);
                    if (friendNotification != null && friendNotification.AccountID_u == accountID)
                    {
                        friendNotification.IsRead_b = true;
                        if (db.SaveChanges() > 0)
                        {
                            return 1;
                        }
                    }
                    return 0;
                }
            }
            catch
            {
                return 101;
            }
        }
        
        public static List<FriendNotification> GetNewFriendNotification(Guid accountID,int skip, int take )
        {
            try
            {
                using (DLULNEntities db = new DLULNEntities())
                {
                    return db.FriendNotifications.Include("Friend.Account").Include("Friend.Account1").Where(fn => fn.AccountID_u == accountID && fn.IsRead_b == false)
                        .OrderByDescending(fn=>fn.CreateDate_dt).Skip(skip).Take(take).ToList();
                }
            }
            catch 
            {
                return null;
            }
        }

        public static List<FriendNotification> GetPager(IQueryable<FriendNotification> friendNotifications, int page, int numberOfItemPerPage, ref int totalItems)
        {
            totalItems = friendNotifications.Count();
            if (page < 1)
            {
                page = 1;
            }

            return friendNotifications.OrderByDescending(fn=>fn.CreateDate_dt).OrderBy(fn=>fn.IsRead_b).Skip((page - 1) * numberOfItemPerPage).Take(numberOfItemPerPage).ToList();
        }

        public static List<FriendNotification> GetAllFriendNotification(Guid accountID,int page, int numberOfItemPerPage,ref int totalItems)
        {
            using (DLULNEntities db = new DLULNEntities())
            {
                IQueryable<FriendNotification> query = db.FriendNotifications.Include("Friend.Account").Include("Friend.Account1").Where(fn => fn.AccountID_u == accountID);
                return GetPager(query, page, numberOfItemPerPage, ref totalItems);
            }
        }

        public static List<FriendNotification> GetLatestFriendNotification(Guid accountID, DateTime time)
        {
            try
            {
                using (DLULNEntities db = new DLULNEntities())
                {
                    return db.FriendNotifications.Include("Friend.Account")
                        .Include("Friend.Account1").OrderBy(fn=>fn.CreateDate_dt)
                        .Where(fn => fn.AccountID_u == accountID && fn.IsRead_b == false && fn.CreateDate_dt.CompareTo(time) > 0).ToList();
                }

            }
            catch 
            {
                return null;   
            }
        }
    }
}
